package com.qf.service.impl;

import com.qf.constant.CacheConstants;
import com.qf.model.StandardSubmit;
import com.qf.service.Strategy;
import com.qf.service.api.CacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 扣费策略
 * @author 何星宇
 * @version V1.0
 * @Date 2021/11/23 19:32
 */

@Service(value = "fee")
@Slf4j
public class FeeStrategy implements Strategy {
    @Autowired
    private CacheService cacheService;
    @Override
    public boolean strategy(StandardSubmit standardSubmit) {
        //记录日志
        log.info("【策略模块-扣费策略】 策略开始执行！");

        //查询客户剩余金额
        String fee1 = cacheService.hGet(CacheConstants.CACHE_PREFIX_CLIENT + standardSubmit.getClientID(), "fee");
        Long balance = Long.parseLong(fee1);
        //查询当前短信费用
        Long fee = standardSubmit.getFee();
        if (balance<fee){
            log.info("【策略模块-扣费策略】 账户余额不足！ 当前余额：{}，短信费用：{}",balance,fee);
            return false;
        }

        //执行预扣费
        cacheService.incr(CacheConstants.CACHE_PREFIX_CUSTOMER_FEE+standardSubmit.getClientID(), fee);
        //记录日志
        log.info("【策略模块-扣费策略】 预扣费完成！ 当前短信费用为：{}",fee);
        return true;
    }
}
